/**
 * 
 */
package com.et114.modules.products.brands.action;

import java.util.List;
import java.util.StringTokenizer;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.et114.components.simpletable.DataList;
import com.et114.core.action.BaseDispatchAction;
import com.et114.core.common.CONST;
import com.et114.core.utility.FileUploadUtil;
import com.et114.core.utility.NumberUtils;
import com.et114.core.utility.StringPool;
import com.et114.core.utility.ThreadSynchronizer;
import com.et114.modules.products.brands.services.BrandServiceImpl;
import com.et114.modules.products.brands.vo.Brand;
import com.et114.modules.products.commons.Const;
import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.vo.BizOption;

/*
 * @author guanhw
 */
public class BrandAction extends BaseDispatchAction {
	@ SuppressWarnings ( "unchecked" )
	public  ActionForward getBrand( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC 
		List< BizOption >  bssOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_PRO_TYPE_KEY ,  true  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "bssOps" , bssOps ) ;
		List< BizOption > csOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_COMMON_STATUS_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "csOps" , csOps ) ;

		//PARAM
		BrandForm brandForm = ( BrandForm )  form ; 
		Long brandId = brandForm.getBrandId ( ) ; 
		//Byte proType = brandForm.getProType ( ) ; 
		Brand vo = new Brand( ) ; 
		PropertyUtils.copyProperties ( vo , brandForm ) ; 
		
		
		//BIS
		BrandServiceImpl brandServiceImpl = ( BrandServiceImpl ) getBean( "brandServiceImpl" ) ;
		
		if ( ! NumberUtils.isEmptyOrZero ( brandId ) ) { // load one
			vo = brandServiceImpl.getBrandById ( brandId ) ;
			request.setAttribute ( "vo" , vo ) ; 
			return mapping.findForward ( "editbrand" ) ;
		}
		else { //loads 
			DataList brands = brandServiceImpl.getBrands ( vo ) ; 
			request.setAttribute ( "vo" , vo ) ; 
			request.setAttribute ( "brands" , brands ) ;
			return mapping.findForward ( "brands" ) ; 
		}
	}
	
	@SuppressWarnings("unchecked")
	public  ActionForward toEditBrand( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		List< BizOption > csOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_COMMON_STATUS_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "csOps" , csOps ) ;

		return mapping.findForward ( "editbrand" ) ;
	}
	
	@ SuppressWarnings ( "unchecked" )
	public  ActionForward editBrand( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
			//DIC 
			List< BizOption >  bssOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_PRO_TYPE_KEY ,  true  , CONST.COUNTRY_ZH  ) ;
			request.setAttribute ( "bssOps" , bssOps ) ;
			//PARAM
			BrandForm brandForm = ( BrandForm )  form ; 
			Long brandId = brandForm.getBrandId ( ) ; 
			Brand vo = new Brand( ) ; 
			PropertyUtils.copyProperties ( vo , brandForm ) ; 
			
			if ( NumberUtils.isEmptyOrZero ( brandId ) ) {
			
			}
			//BIS
			BrandServiceImpl brandServiceImpl = ( BrandServiceImpl ) getBean( "brandServiceImpl" ) ;
			
			
			
			brandId = saveAndUpdateBrand( vo , brandForm.getImagePathff ( ) , brandServiceImpl , request ) ; 
			
			return new ActionForward("brand.go?method=getBrand&brandId=" + brandId
					+ "&pageNum=" + brandForm.getPageNum(), true);
	}

	public  ActionForward removeBrand( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
			//PARAM
			BrandForm brandForm = ( BrandForm )  form ; 
			Byte proType = brandForm.getProType() ; 
			if ( NumberUtils.isEmptyOrZero ( proType ) )
				throw new Exception ( "proType is NULL !" ) ; 
			
			BrandServiceImpl brandServiceImpl = ( BrandServiceImpl ) getBean( "brandServiceImpl" ) ;
			Long[] deleteIds = brandForm.getDeleteId() ; 
			//Brand brand = null ; 
			if ( deleteIds != null ) {
				for ( Long deleteId : deleteIds ) {
					//brand = brandServiceImpl.getBrandById( deleteId ) ; 
					//brand.setRemoveMark( String.valueOf ( Const.COMMON_REMOVE_MARK_Y ) ) ; 
					//brandServiceImpl.updateBrand( brand ) ; 
					brandServiceImpl.removeBrandById ( deleteId ) ;
				}
			}

		return new ActionForward("brand.go?method=getBrand&proType=" + proType
				+ "&pageNum=" + brandForm.getPageNum(), true);
	}	
	
	public  ActionForward toDisOrderBrand( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
			//PARAM
			BrandForm brandForm = ( BrandForm )  form ; 
			Byte proType = brandForm.getProType() ; 
			if ( NumberUtils.isEmptyOrZero ( proType ) )
				throw new Exception ( "proType is NULL !" ) ; 
	
			Brand vo = new Brand( ) ; 
			PropertyUtils.copyProperties ( vo , brandForm ) ; 

			BrandServiceImpl brandServiceImpl = ( BrandServiceImpl ) getBean( "brandServiceImpl" ) ;
			List< Brand > brands = brandServiceImpl.getBrandList ( vo ) ; 
			
			request.setAttribute ( "vo" , vo ) ; 
			request.setAttribute ( "brands" , brands ) ;
			return mapping.findForward ( "disOrder" ) ; 
	}
	
	public  ActionForward disOrderBrand( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
			//PARAM
			BrandForm brandForm = ( BrandForm )  form ; 
			Byte proType = brandForm.getProType() ; 
			if ( NumberUtils.isEmptyOrZero ( proType ) )
				throw new Exception ( "proType is NULL !" ) ; 
			
			//long funcNo = 0 ; 
			BrandServiceImpl brandServiceImpl = ( BrandServiceImpl ) getBean( "brandServiceImpl" ) ;
			String orders = brandForm.getOrders() ; 
			if ( orders != null ) {
				StringTokenizer st = new StringTokenizer( orders , StringPool.SEMICOLON ) ;
				int count = st.countTokens ( ) ; 
				Brand vo = new Brand( ) ; 
				for (  int i = 0 ; i <  count ; i ++ ) {
					Long orderId = Long.valueOf ( st.nextToken() );
					vo = brandServiceImpl.getBrandById( orderId ) ; 
					vo.setDisOrder ( Long.valueOf ( i + 1 ) ) ; 
					brandServiceImpl.updateBrand ( vo ) ;
				}
			}
			return new ActionForward( "brand.go?method=toDisOrderBrand&proType=" + proType , true ) ;
	}
	
	private Long saveAndUpdateBrand ( Brand brand , FormFile formFile ,
			BrandServiceImpl brandServiceImpl , HttpServletRequest request )
			throws Exception {
		Long brandId = brand.getBrandId ( ) ; 
		String uploadFn = formFile.getFileName ( );
		if ( ! StringUtils.isEmpty ( uploadFn )
				&& ! StringUtils.isBlank ( uploadFn ) ) {
			FileUploadUtil upload = new FileUploadUtil ( );
			upload.setRestrictfs ( 102400 * 3 ) ;
			ThreadSynchronizer.threadEnter ( );
			try {
				String fn = upload.uploadv2 ( formFile , request ,
						Const.PRO_BRANDS_IMG_UPLOAD_PATH , UUID.randomUUID ( )
								.toString ( ) );
				fn = Const.PRO_BRANDS_IMG_UPLOAD_PATH + fn;
				brand.setImagePath ( fn ) ; 
			} finally {
				ThreadSynchronizer.threadExit ( );
			}
		}		
		try {
			if ( ! NumberUtils.isEmptyOrZero ( brandId ) ) { // update
				Brand vo = brandServiceImpl.getBrandById ( brandId ) ; 
				String prop = vo.getImagePath ( ) ; 
				if ( ! StringUtils.isEmpty ( uploadFn )
						&& ! StringUtils.isBlank ( uploadFn ) ) { // 存在图片 删除
					if ( ! StringUtils.isEmpty ( prop )
							&& ! StringUtils.isBlank ( prop ) ) {
						FileUploadUtil.removeRealPathFile ( prop , request );
					}
					
				} else
					// 没有则 复制
					brand.setImagePath ( prop ) ; 
				brandServiceImpl.updateBrand (  brand ) ;
			} else {
				brandId = brandServiceImpl.saveBrand ( brand ) ;
				brand.setDisOrder ( brandId ) ; 
				brandServiceImpl.updateBrand ( brand ) ;
			}
		}
		
		catch ( Exception ex ) {
			logger.error ( ex.getMessage ( ) ,ex ) ;
			
			throw new Exception( "ERROR!" ) ;
		}

		
		return brandId ; 
	}		
}
